<?php

class CommonAction extends Action {
    
    /**
     * 系統語言
     */
    protected $language;
    
    public function _initialize() {
        $this->setLayout();
        
        $this->assign('act', MODULE_NAME);
        $this->assign('meth', ACTION_NAME);
        $this->language = C('DEFAULT_LANG');
        $this->assign('language', $this->language);
        $this->assign('homePage', false);

        //$this->getLastNews();
        $this->getNav();
        $this->getAd();
    }

   

    public function setLayout() {
        $layout = $this->_get('layout');
        if ($layout) {
            C('LAYOUT_NAME', 'Layout/' . $layout);
        }
        $this->assign('layout_name', C('LAYOUT_NAME'));
    }
    /**
     * 獲取最新新聞
     */
    public function getLastNews(){
        $model = M('News');
        $news = $model->order('news_id desc')->limit(4)->select();
        $this->assign('lastnews',$news);
    }
    
    /**
     * 獲取首頁二級導航
     */
    public function getNav(){
        $category = M('Category');
        $navs = $category->where("parent_id = 2 and status = 1 and language = '$this->language'")->order('position asc')->select();

        $this->assign('topnavs',$navs);
    }
    /**
     * 獲取廣告
     */
    public function getAd(){
        $model = M('Advert');
        $rows = $model->where("status = 1 and language = '$this->language'")->order('RAND()')->limit(2)->select();
        $this->assign('ads',$rows);
    }
    /**
     * 根據分類標識獲取該分類下面的一篇文章
     * @param string $identify
     * @return array
     */
    public function getCategoryArticle($identify){
        $categoryModel = M('Category');
        $map = array();
        $map['language'] = $this->language;
        $map['identify'] = $identify;
        $category = $categoryModel->where($map)->find();
        $art = M('Article');
        $mapArticle = array();
        $mapArticle['category_id'] = $category['category_id'];
        $mapArticle['language'] = $this->language;
        $row = $art->where($mapArticle)->order('article_id desc')->find();
        $return['category'] = $category;
        $return['row'] = $row;
        return $return;
    }

}
